/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.config.web.email;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.config.entity.email.ConfigEmail;
import com.thinkgem.jeesite.modules.config.service.email.ConfigEmailService;

/**
 * 邮箱配置Controller
 *
 * @author JiangYongKang
 * @version 2016-12-30
 */
@Controller
@RequestMapping(value = "${adminPath}/config/email/configEmail")
public class ConfigEmailController extends BaseController {

    @Autowired
    private ConfigEmailService configEmailService;

    @ModelAttribute
    public ConfigEmail get(@RequestParam(required = false) String id) {
        ConfigEmail entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = configEmailService.get(id);
        }
        if (entity == null) {
            entity = new ConfigEmail();
        }
        return entity;
    }

    @RequiresPermissions("config:email:configEmail:view")
    @RequestMapping(value = {"list", ""})
    public String list(ConfigEmail configEmail, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ConfigEmail> page = configEmailService.findPage(new Page<ConfigEmail>(request, response), configEmail);
        model.addAttribute("page", page);
        return "modules/config/email/configEmailList";
    }

    @RequiresPermissions("config:email:configEmail:view")
    @RequestMapping(value = "form")
    public String form(ConfigEmail configEmail, Model model) {
        model.addAttribute("configEmail", configEmail);
        return "modules/config/email/configEmailForm";
    }

    @RequiresPermissions("config:email:configEmail:edit")
    @RequestMapping(value = "save")
    public String save(ConfigEmail configEmail, Model model, RedirectAttributes redirectAttributes) {
        configEmailService.save(configEmail);
        addMessage(redirectAttributes, "保存邮箱配置成功");
        return "redirect:" + Global.getAdminPath() + "/config/email/configEmail/?repage";
    }

    @RequiresPermissions("config:email:configEmail:edit")
    @RequestMapping(value = "delete")
    public String delete(ConfigEmail configEmail, RedirectAttributes redirectAttributes) {
        configEmailService.delete(configEmail);
        addMessage(redirectAttributes, "删除邮箱配置成功");
        return "redirect:" + Global.getAdminPath() + "/config/email/configEmail/?repage";
    }

}